<?php
namespace app\controller\app;

use app\BaseController;
use app\model\Elevator;
use app\model\Project;
use app\model\TowerCrane;
use app\model\TowerCar;
use app\model\CraneCar;
use app\model\User;
use app\model\WorkOrder;
use app\model\WorkOrderList;
use think\Request;

class ProjectController extends BaseController
{
    //添加项目
    public function addProject(Request $request)
    {

        $user = new Project;
        $data = $request->post();
        foreach ($data as $key => $value) {
            $user->$key = $value;
        }
        $rq = $user->save();
        if ($rq) {
            return success($data);
        }
    }
    public function updateProject(Request $request)
    {
        $user = Project::find($request->post('id'));
        $data = $request->post();

        if ($user) {
            foreach ($data as $key => $value) {
                $user->$key = $value;
            }
        }
        $rq = $user->save();

        if ($data['pname']) { //更新设备项目名
            // $TowerCrane = TowerCrane::where('pid', $data['id'])->find();
            // if ($TowerCrane) {
            //     $TowerCrane->pname = $data['pname'];
            //     $rq = $TowerCrane->save();
            // }
            Elevator::where('pid', $data['id'])->update(['pname' => $data['pname']]);
            TowerCrane::where('pid', $data['id'])->update(['pname' => $data['pname']]);
            // $Elevator = Elevator::where('pid', $data['id'])->find();

            // if ($Elevator) {
            //     $Elevator->pname = $data['pname'];
            //     $rq = $Elevator->save();
            // }

        }

        if ($rq) {
            return success($data);
        }
    }
    public function delUser(Request $request)
    {
        $id = $request->post('id');
        $rq = User::destroy($id);
        if ($rq) {
            return success($request);
        }
    }
    // 项目信息
    public function projectInfo(Request $request)
    {
        $Project = Project::find($request->param('id'));
        $hidden = ['uid', 'belong_user', 'create_time', 'update_time', 'delete_time'];
        $Project->hidden($hidden);
        // $Project['list'] =[];
        return success($Project);
    }
    // 项目列表
    public function projectList(Request $request)
    {
        $where = [];
        $where[] = ['belong_user', 'find in set', $request->param('username')];
        $Project = Project::where($where)->order('id desc')->select();
        return success($Project);
    }
    // 项目-设备统计信息
    public function equipmentCountInfo(Request $request)
    {
        $equipmentType = $request->param('equipmentType');
        $modelName = 'app\model\\' . $equipmentType;
        $equipmentModel = new $modelName;
        $where = [];
        $where[] = ['pid', '=', $request->param('pid')];
        if ($request->has('model', 'get', true)) {
            $model = $request->param('model');
            $where[] = ['model', '=', $model];
        }
        if ($request->has('install_status', 'get', true)) {
            $install_status = $request->param('install_status');
            $where[] = ['install_status', '=', $install_status];
        }
        $data['list'] = $equipmentModel::where($where)->order('bind_time desc')->field('eid,ename,sim,install_status,model,install_time,bind_time')->select();
        // if ($data['list']->isEmpty()) {
        //     return success(null);
        // }
        $data['install_status']['installed'] = $data['list']->where('install_status', 1)->count();
        $data['install_status']['install_wait'] = $data['list']->where('install_status', 0)->count();
        $data['install_status']['repair'] = $data['list']->where('install_status', 3)->count();
        $data['install_status']['repair_wait'] = $data['list']->where('install_status', 2)->count();
        // ksort($data);//交换位置
        return success($data);
    }
    // 项目-设备待安装,待维修查询
    public function equipmentInstallStatus(Request $request)
    {
        /* TowerCrane,Elevator,TowerCar,CraneCar */
        $where[] = ['pid', '=', $request->param('pid')];
        $TowerCrane = TowerCrane::where($where)->field('install_status')->select();
        $Elevator = Elevator::where($where)->field('install_status')->select();
        $TowerCar = TowerCar::where($where)->field('install_status')->select();
        $CraneCar = CraneCar::where($where)->field('install_status')->select();


        $data['towerCrane']['installed'] = $TowerCrane->where('install_status', 1)->count();
        $data['towerCrane']['install_wait'] = $TowerCrane->where('install_status', 0)->count();
        $data['towerCrane']['repair'] = $TowerCrane->where('install_status', 3)->count();
        $data['towerCrane']['repair_wait'] = $TowerCrane->where('install_status', 2)->count();
        $data['elevator']['installed'] = $Elevator->where('install_status', 1)->count();
        $data['elevator']['install_wait'] = $Elevator->where('install_status', 0)->count();
        $data['elevator']['repair'] = $Elevator->where('install_status', 3)->count();
        $data['elevator']['repair_wait'] = $Elevator->where('install_status', 2)->count();
        $data['towerCar']['installed'] = $TowerCar->where('install_status', 1)->count();
        $data['towerCar']['install_wait'] = $TowerCar->where('install_status', 0)->count();
        $data['towerCar']['repair'] = $TowerCar->where('install_status', 3)->count();
        $data['towerCar']['repair_wait'] = $TowerCar->where('install_status', 2)->count();
        $data['craneCar']['installed'] = $CraneCar->where('install_status', 1)->count();
        $data['craneCar']['install_wait'] = $CraneCar->where('install_status', 0)->count();
        $data['craneCar']['repair'] = $CraneCar->where('install_status', 3)->count();
        $data['craneCar']['repair_wait'] = $CraneCar->where('install_status', 2)->count();

        return success($data);

        
    }
    //项目-设备安装图片
    public function addEquipmentInstallImg(Request $request)
    {
        $data = $request->post();
        $WorkOrder = WorkOrder::where('eid', $data['eid'])->find();
        if (!$WorkOrder) {
            $WorkOrder = new WorkOrder;
        }
        foreach ($data as $key => $value) {
            $WorkOrder->$key = $value;
        }
        //创建model
        $equipmentType = $request->param('equipmentType');
        $modelName = 'app\model\\' . $equipmentType;
        $equipmentModel = new $modelName;
        //查询设备
        $equipment = $equipmentModel::where('eid', $request->param('eid'))->find();
        if ($equipment) {
            if (isset($data['install_img'])) {
                $equipment->install_status = 1; //已安装
            }
            // if (isset($data['repair_img'])) {
            //     $equipment->install_status = 2; //待维修
            //     $equipment->repair_location = $request->param('repair_location'); //待维修
            // }
            if (isset($data['repairEnd_img'])) {
                $equipment->install_status = 3; //已维修
                $equipment->repair_location = $request->param('repair_location'); //已维修
            }
            $WorkOrder->type = 99; //设备安装图
            $rq = $WorkOrder->save();
            $rq = $equipment->save();
        }
        if (isset($rq)) {
            return success($data);
        } else {
            return fail(null, '无该设备');
        }
    }
    // 设备图片
    public function equipmentInstallImg(Request $request)
    {
        $equipmentType = $request->param('equipmentType');
        $modelName = 'app\model\\' . $equipmentType;
        $equipmentModel = new $modelName;
        $where = [];
        $where[] = ['eid', '=', $request->param('eid')];
        $list = $equipmentModel::where($where)->field('repair_location,install_status')->find();
        $list['img'] = WorkOrder::where($where)->find();
        return success($list);
    }
    // 添加派工单
    public function addWorkOrderList(Request $request)
    {

        $WorkOrderList = new WorkOrderList;
        $Project = Project::find($request->post('pid'));
        $data = $request->post();
        $data['pname'] = $Project->pname;
        $data['location'] = $Project->location;
        $data['project_people'] = $Project->project_people;
        $data['project_tel'] = $Project->project_tel;
        foreach ($data as $key => $value) {
            $WorkOrderList->$key = $value;
        }
        $rq = $WorkOrderList->save();
        if ($rq) {
            return success($data);
        }
    }
    // 跟新设备安装状态 提交维修申请
    public function applyRepair(Request $request)
    {
        //创建model
        $equipmentType = $request->param('equipmentType');
        $modelName = 'app\model\\' . $equipmentType;
        $equipmentModel = new $modelName;
        //查询设备
        $equipment = $equipmentModel::where('eid', $request->param('eid'))->find();
        $equipment->repair_location = $request->param('repair_location'); //已维修
        $equipment->install_status = 2;
        $rq = $equipment->save();
        return success($rq);
    }
}
